package com.tanhua.dubbo.utils;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SaveTimeLine {


    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    public void saveTimeLine(Long userId, ObjectId movementId) throws InterruptedException {

       // Thread.sleep(10000);

        //2.查询当前登录用户的所有好友
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);


        //3.遍历当前用户的好友
        for (Friend friend : friends) {
            //创建时间线表
            MovementTimeLine mtl = new MovementTimeLine();
            mtl.setUserId(friend.getUserId());
            mtl.setFriendId(friend.getFriendId());
            mtl.setMovementId(movementId);
            mtl.setCreated(System.currentTimeMillis());

            //保存到时间线表
            mongoTemplate.save(mtl);
        }
    }
}
